<%
response.setHeader("Cache-Control", "no-cache, no-store, must-revalidate"); //HTTP 1.1
response.setHeader("Pragma","no-cache"); //HTTP 1.0
response.setDateHeader ("Expires", -1); //prevents caching at the proxy server
%>
<%@page contentType="text/html; charset=iso-8859-1" language="java" import="java.sql.*,java.io.*"%>
<%@ include file="Connections/conDB.jsp" %>
<% int validaSession = 2; %>
<%@ include file="Connections/session.jsp" %>

<%
String parId = request.getParameter("id");
String rsNombre_fis = "";
File varFile = null;
String physicalFilePath = application.getRealPath("/")+"\\files";

Driver DriverRs = (Driver)Class.forName(MM_conDB_DRIVER).newInstance();
Connection ConnRs = DriverManager.getConnection(MM_conDB_STRING,MM_conDB_USERNAME,MM_conDB_PASSWORD);
String query ="";
PreparedStatement StatementRs =null;
ResultSet Rsquery = null;
boolean Rsquery_hasData = false;

if (parId!=null && !parId.equals("")) {
	// busca el nombre fisico en docto_elect
	query = "SELECT nombre_fis FROM docto_elect WHERE id_doc = "+parId;
	//out.print(query);
	StatementRs = ConnRs.prepareStatement(query);
	Rsquery = StatementRs.executeQuery();
	Rsquery_hasData = Rsquery.next();
	if (Rsquery_hasData) {
		try {
			// elimina el archivo fisico
			varFile = new File(physicalFilePath+"\\"+Rsquery.getString("nombre_fis"));
			//out.print(physicalFilePath+"\\"+Rsquery.getString("nombre_fis"));
			varFile.delete();
			// elimina el registro en docto_elect
			query= " DELETE docto_elect WHERE id_doc = "+parId;
			StatementRs=ConnRs.prepareStatement(query);
		   	StatementRs.executeUpdate();

		} catch (Exception e) {
			out.print("<script>");
			out.print("alert('Error al eliminar el archivo.'"+e+"');");
			out.print("window.close();");
			out.print("</script>");
		}
	}
	Rsquery.close();
}

	out.print("<script>");
	out.print("window.opener.location.reload();");
	out.print("window.close();");
	out.print("</script>");

ConnRs.close();
%>
<html>
<head>
<title>CIMA v2 - Eliminar Archivo</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
</head>

<body bgcolor="#FFFFFF" text="#000000">

</body>
<HEAD>
<META HTTP-EQUIV="PRAGMA" CONTENT="NO-CACHE">
</HEAD>
</html>
